<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Model_request extends CI_Model {

    function __construct() {
        parent::__construct();
    }

    public function cek_double_update($id_request, $table) {
        $last_status = $this->db->query("select status from $table where id_request = ?", array($id_request))->row()->status;
        return $last_status;
    }
    
    public function update_token($primary_key, $token){        
        $this->db->query("update request_pin set eksekutor=?,tanggal_eksekusi=now(), key_token=? where id_request=?",array("ADMIN", $token, $primary_key));        
    }
    
    public function approve_poin($primary_key, $id_trx){        
        $this->db->query("update request_poin set eksekutor=?,tanggal_eksekusi=now() where id_request=?",array("ADMIN", $primary_key));
    }
    
    public function get_data_member($primary_key, $table){
        return $this->db->query("select id_member,alias_member from $table where id_request=?",array($primary_key))->row();        
    }
    
    // registrasi via menu admin dihitung sebagai transaksi, karena melibatkan pertukaran uang tunai dengan id baru
    public function insert_transaksi($post_data) {        
        $post_data[] = date("Y-m-d", strtotime("today"));
        $post_data[] = date("H:i:s", strtotime("now"));      
        $this->db->query("insert into transaksi"
                . "(id_member,alias_member,produk,data_1,data_2,data_3,status,jumlah_beli,harga_beli,harga_jual,tanggal_transaksi,waktu_transaksi) values "
                . "(?,?,?,?,?,?,?,?,?,?,?,?) ", $post_data);
        return $this->db->insert_id();
    }
    
    public function get_data_request_dinar() {
        global $g_list_status_dinar;
        $list_status = $g_list_status_dinar;
        $data = array();
        foreach($list_status as $status => $status_name) {
            if ($status <> 4) {
                $data_per_status = array();
                $sql = "select count(*) as jumlah from request_dinar where status = $status";
                $query = $this->db->query($sql);
                foreach ($query->result() as $row) {
                    $data_per_status["jumlah"] = $row->jumlah;
                }
                $data_per_status["status_name"] = $status_name;
                $sql = "select r.*, m.nickname from request_dinar r left join member m on r.id_member = m.id_member where r.status = $status";
                $query = $this->db->query($sql);
                $data_per_status["data"] = $query;
                array_push($data, $data_per_status);
            }
        }        
        return $data;
    }
    
    public function get_data_request_pesawat() {
        global $g_list_status_pesawat;
        $list_status = $g_list_status_pesawat;
        $data = array();
        foreach($list_status as $status => $status_name) {
            if ($status <> 4) {
                $data_per_status = array();
                $sql = "select count(*) as jumlah from request_pesawat where status = $status";
                $query = $this->db->query($sql);
                foreach ($query->result() as $row) {
                    $data_per_status["jumlah"] = $row->jumlah;
                }
                $data_per_status["status_name"] = $status_name;
                $sql = "select r.*, m.nickname from request_pesawat r left join member m on r.id_member = m.id_member where r.status = $status";
                $query = $this->db->query($sql);
                $data_per_status["data"] = $query;
                array_push($data, $data_per_status);
            }
        }               
        return $data;
    }
    
    public function get_data_request_hotel() {
        global $g_list_status_hotel;
        $list_status = $g_list_status_hotel;
        $data = array();
        foreach($list_status as $status => $status_name) {
            if ($status <> 4) {
                $data_per_status = array();
                $sql = "select count(*) as jumlah from request_hotel where status = $status";
                $query = $this->db->query($sql);
                foreach ($query->result() as $row) {
                    $data_per_status["jumlah"] = $row->jumlah;
                }
                $data_per_status["status_name"] = $status_name;
                $sql = "select r.*, m.nickname from request_hotel r left join member m on r.id_member = m.id_member where r.status = $status";
                $query = $this->db->query($sql);
                $data_per_status["data"] = $query;
                array_push($data, $data_per_status);
            }
        }               
        return $data;
    }
    
    public function get_data_request_seminar() {
        global $g_list_status_seminar;
        $list_status = $g_list_status_seminar;        
        $data = array();
        foreach($list_status as $status => $status_name) {
            if ($status <> 4) {
                $data_per_status = array();
                $sql = "select count(*) as jumlah from request_seminar where is_registered = $status";
                $query = $this->db->query($sql);
                foreach ($query->result() as $row) {
                    $data_per_status["jumlah"] = $row->jumlah;
                }
                $data_per_status["status_name"] = $status_name;
                $sql = "select r.*, m.judul from request_seminar r join data_seminar m on r.id_seminar = m.id_seminar where r.is_registered = $status";
               
                $query = $this->db->query($sql);
                $data_per_status["data"] = $query;
                array_push($data, $data_per_status);
            }
        }        
        return $data;
    }
    
    public function get_data_redeem_tour($is_tour_khusus = 0) {
        global $g_list_status_redeem;
        $list_status = $g_list_status_redeem;
        $data = array();
        foreach($list_status as $status => $status_name) {
            if ($status <> 4) {
                $data_per_status = array();
                $sql = "select count(*) as jumlah from v_request_tour where is_processed = $status and is_special_tour = ".$is_tour_khusus;
                $query = $this->db->query($sql);
                foreach ($query->result() as $row) {
                    $data_per_status["jumlah"] = $row->jumlah;
                }
                $data_per_status["status_name"] = $status_name;
                $sql = "select * from v_request_tour where is_processed = $status and is_special_tour = ".$is_tour_khusus;
                $query = $this->db->query($sql);
                $data_per_status["data"] = $query;
                array_push($data, $data_per_status);
            }
        }        
        return $data;
    }
    
    public function getFotoSeminar($id_seminar){
        $this->db->select("foto");
        $this->db->from("data_seminar");
        $this->db->where(array(
            "id_seminar" => $id_seminar
        ));
        
        return $this->db->get()->row();
    }
    
    public function updatePathFoto($id_seminar, $path){
        $this->db->update("data_seminar", array("foto" => $path), array("id_seminar" => $id_seminar));
    }
}